#include "resultimageitem.h"
#include <QPen>

ResultImageItem::ResultImageItem(QWidget *parent) : QGraphicsPixmapItem(nullptr){
    m_nImageResult = 0;

    InitDatas();

    /*this->setFlag(QGraphicsItem::ItemIsSelectable);
    this->setFlag(QGraphicsItem::ItemIsMovable);
    this->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
    this->setFlag(QGraphicsItem::ItemSendsScenePositionChanges);

    // Put this in the constructor of your QGraphicsItem
    setCacheMode( QGraphicsItem::DeviceCoordinateCache );
    // or
    setCacheMode( QGraphicsItem::ItemCoordinateCache);*/
}

void ResultImageItem::InitDatas(){
    // (1)
    m_objLineLeftTop.append(QPoint(9,211));
    m_objLineLeftTop.append(QPoint(9,60));
    m_objLineLeftTop.append(QPoint(104,5));
    m_objLineLeftTop.append(QPoint(325,5));

    //(2)
    m_objBorder.append(QPoint(32,245));
    m_objBorder.append(QPoint(32,73));
    m_objBorder.append(QPoint(116,18));
    m_objBorder.append(QPoint(545,18));
    m_objBorder.append(QPoint(545,350));
    m_objBorder.append(QPoint(482,389));
    m_objBorder.append(QPoint(73,389));
    m_objBorder.append(QPoint(73,269));
    m_objBorder.append(QPoint(32,245));

    //(3)
    for(int i = 0;i < 10;i++){
        m_lstDecoration.append(QPoint(32,263 + i * 12));
        m_lstDecoration.append(QPoint(59,277 + i * 12));
    }

    //(4)
    m_lstTriangle.append(QPoint(525,390));
    m_lstTriangle.append(QPoint(570,360));
    m_lstTriangle.append(QPoint(570,390));

    //(5) (7)
    m_lstCenters.append(QPoint(9,211));
    m_lstCenters.append(QPoint(9,227));
    m_lstCenters.append(QPoint(9,243));
    m_lstCenters.append(QPoint(590,212));
    m_lstCenters.append(QPoint(590,196));
    m_lstCenters.append(QPoint(590,180));

    //(6)
    m_objLineRightBottom.append(QPoint(590,212));
    m_objLineRightBottom.append(QPoint(590,395));
    m_objLineRightBottom.append(QPoint(425,396));

    // (7)
    m_ptText = QPoint(450,70);
}

void ResultImageItem::SetImageResult(int nRet){
    m_nImageResult = nRet;
}

QRectF ResultImageItem::boundingRect() const
{
    QRectF rect = this->childrenBoundingRect();

    return rect;
}

void ResultImageItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget){
    //painter->setClipRect(this->boundingRect());
    QGraphicsPixmapItem::paint(painter, option, widget);

    if(m_nImageResult == -1){
        QPen pen(Qt::red);
        pen.setWidth(2);
        painter->setPen(pen);
        painter->setBrush(QBrush(Qt::red));
    }else if(m_nImageResult == 1){
        QPen pen(Qt::green);
        pen.setWidth(2);
        painter->setPen(pen);
        painter->setBrush(QBrush(Qt::green));
    }else{
        return ;
    }

    painter->drawPolyline(m_objLineLeftTop);
    painter->drawPolyline(m_objBorder);
    painter->drawLines(m_lstDecoration);
    painter->drawPolygon(m_lstTriangle);
    painter->drawPolyline(m_objLineRightBottom);  

    for(int i = 0;i < m_lstCenters.size(); i++){
        painter->drawEllipse(m_lstCenters.at(i),4,4);
    }

    painter->setFont(QFont("Arial",40,QFont::Bold));
    if(m_nImageResult == -1){
        painter->drawText(m_ptText,QStringLiteral("NG"));
    }else if(m_nImageResult == 1){
        painter->drawText(m_ptText,QStringLiteral("OK"));
    }
}
